package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopBusinessDao;
import ysq.xyg.shop.model.po.ShopBusiness;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.shop.model.vo.ShopBusinessVo;
import ysq.xyg.shop.service.ShopBusinessService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 商城模块-交易表service实现
 *
 * @author congcong tao
 * @date 2019-4-28 11:19:14
 */
@Service("shopBusinessService")
public class ShopBusinessServiceImpl extends BaseServiceImpl<ShopBusiness> implements ShopBusinessService {

    @Resource
    private ShopBusinessDao shopBusinessDao;

    private ShopBusiness shopBusiness;

    @Autowired
    public void setBaseDao(ShopBusinessDao shopBusinessDao) {
        this.baseDao = shopBusinessDao;
    }

    /**
     * 分页查询
     *
     * @param shopBusinessVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:14
     */
    @Override
    public ReturnEntity page(ShopBusinessVo shopBusinessVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopBusiness> list = acceptList(shopBusinessVo);
        PageInfo<ShopBusiness> page=new PageInfo<ShopBusiness>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopBusinessVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:14
     */
    @Override
    public ReturnEntity addOrUpdate(ShopBusinessVo shopBusinessVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopBusinessVo.getId())){
            shopBusiness=new ShopBusiness(currentUser);
            isUpate=false;
        }else{
            shopBusiness=selectByPrimaryKey(shopBusinessVo.getId());
            if(ObjectUtil.isNull(shopBusiness)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopBusiness.setUpdateTime(new Date());
            shopBusiness.setUpdateUser(currentUser.getId());
        }
        shopBusiness.setMemberId(shopBusinessVo.getMemberId());
        shopBusiness.setMoney(shopBusinessVo.getMoney());
        shopBusiness.setType(shopBusinessVo.getType());
        shopBusiness.setDescription(shopBusinessVo.getDescription());
        shopBusiness.setManageTime(shopBusinessVo.getManageTime());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopBusiness);
        }else{
            num=insert(shopBusiness);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:14
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopBusiness shopBusiness=selectByPrimaryKey(id);
        if(shopBusiness!=null){
            shopBusiness.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopBusiness.setUpdateTime(new Date());
            shopBusiness.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopBusiness);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopBusinessVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-4-28 11:19:14
     */
    @Override
    public List<ShopBusiness> acceptList(ShopBusinessVo shopBusinessVo){
        Example example = new Example(ShopBusiness.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopBusinessVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopBusinessVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return shopBusinessDao.selectByExample(example);
    }

    @Override
    public List<ShopBusiness> selectEntityList(ShopBusinessVo shopBusinessVo) {
        return shopBusinessDao.selectEntityList(shopBusinessVo);
    }
}
